'use strict'

class video {
  constructor() {
    this.param = '/'
  }

  async get(c) {
    let ret = await c.service.psql.query(`select * from video order by video_id asc`)
    c.status(200).send(ret.rows)
  }

  async post(c) {
    var obj = c.body
    console.log(obj)
    if (obj) {
      var { video_author, video_time, video_url } = obj
      let row = await c.service.psql.query(`select * from video order by video_id asc`)
      let video_id = parseInt(row.rows[row.rows.length - 1].video_id) + 1
      console.log(video_id, video_author, video_time, video_url)
      let ret = await c.service.psql.query(`insert into video values('${video_id}','${video_author}', '${video_time}', '${video_url}')`)
      console.log(ret)
      c.status(200).send('已接收')
    } else {
      c.status(400).send('please check your data :)')
    }
  }

  async delete(c) {
    console.log(c)
    if (!c.body.isRows) {
      var id = c.body.id
      console.log(id)
      if (id) {
        let ret = await c.service.psql.query(`delete from video where video_id like '${id}'`)
        let ret2 = await c.service.psql.query(`delete from collectvideo where collectvideo_id like '${idArr[i]}'`)
        console.log(ret, ret2)
        c.send('已删除')

      } else {
        c.send('删除失败')
      }
    } else {
      var idArr = c.body.idArr
      console.log(idArr)
      for (let i = 0; i < idArr.length; i++) {
        let ret = await c.service.psql.query(`delete from video where video_id like '${idArr[i]}'`)
        let ret2 = await c.service.psql.query(`delete from collectvideo where collectvideo_id like '${idArr[i]}'`)
        console.log(ret, ret2)
      }
      c.send('complete')
    }
  }

  async put(c) {
    var req = c.body
    console.log(c, req)
    if (req.video_author) {
      let ret = await c.service.psql.query(`update video set video_author = '${req.video_author}' where video_id like '${req.video_id}'`)
      console.log('author', ret)
    }
    if (req.video_time) {
      let ret = await c.service.psql.query(`update video set video_time = '${req.video_time}' where video_id like '${req.video_id}'`)
      console.log('time', ret)
    }
    if (req.video_url) {
      let ret = await c.service.psql.query(`update video set video_url = '${req.video_url}' where video_id like '${req.video_id}'`)
      console.log('url', ret)
    }
    c.send('complete')
  }
}

module.exports = video
